package com.android.takagism.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.takagism.utils.DbUtils;
import com.android.takagism.utils.SensorPackage;

import java.io.ObjectOutputStream;
import java.sql.Timestamp;
import java.util.HashMap;

public class SensorPackageDao {
    private SensorPackageDao() {
    }

    public static void insert(SensorPackage sensorPackage) {
        DbUtils instance = DbUtils.getInstance();
        SQLiteDatabase db = instance.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("dataType",sensorPackage.getDataType());
        contentValues.put("dataString",sensorPackage.getDataString());
        contentValues.put("data",sensorPackage.getData().toString());
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        contentValues.put("time",timestamp.getTime());
        db.insert("sensor_package", null, contentValues);
    }

    public static ContentValues queryManage(){
        SQLiteDatabase dbr = DbUtils.getInstance().getReadableDatabase();
        Cursor save_manage = dbr.query("save_manage", new String[]{
                "id", "deng", "wen_shi_du", "zi_dong_jia_shi", "zi_dong_tiao_wen"
                , "threshold_distance", "threshold_illumination", "threshold_vibration"
        }, "id = ?", new String[]{"0"}, null, null, null);
        if (save_manage.moveToFirst()){
            ContentValues contentValues = new ContentValues();
            int threshold_distance = save_manage.getInt(save_manage.getColumnIndex("threshold_distance"));
            int threshold_illumination = save_manage.getInt(save_manage.getColumnIndex("threshold_illumination"));
            int threshold_vibration = save_manage.getInt(save_manage.getColumnIndex("threshold_vibration"));

            contentValues.put("deng", save_manage.getInt(save_manage.getColumnIndex("deng")));
            contentValues.put("wen_shi_du", save_manage.getString(save_manage.getColumnIndex("wen_shi_du")));
            contentValues.put("zi_dong_jia_shi", save_manage.getInt(save_manage.getColumnIndex("zi_dong_jia_shi")));
            contentValues.put("zi_dong_tiao_wen", save_manage.getInt(save_manage.getColumnIndex("zi_dong_tiao_wen")));
            contentValues.put("threshold_distance", threshold_distance);
            contentValues.put("threshold_illumination", threshold_illumination);
            contentValues.put("threshold_vibration", threshold_vibration);
            return contentValues;
        }
        return null;
    }

    public static void updateManage(ContentValues contentValues){

        SQLiteDatabase dbw = DbUtils.getInstance().getWritableDatabase();
        dbw.update("save_manage", contentValues, "id = ?", new String[]{"0"});
    }

    public static void insertManage(ContentValues contentValues){
        contentValues.put("id","0");
        SQLiteDatabase dbw = DbUtils.getInstance().getWritableDatabase();
        dbw.insert("save_manage", null, contentValues);
    }
}
